﻿//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     代理业务类库
//     日结算业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using CL.Business.DAL;
using CL.Business.Entity;
using CL.Enum.Common;
using CL.View.Entity.Business;
using CL.Entity.Json.WebAPI;
using CL.Tools.Common;

namespace CL.Business.BLL
{
    /// <summary>
    /// SettleDayBLL 业务
    /// </summary>
    public class SettleDayBLL
    {
        SettleDayDAL dal = new SettleDayDAL(DbConnectionEnum.CaileBusiness);

        /// <summary>
        /// 查询视图对象
        /// </summary>
        /// <param name="AccountID"></param>
        /// <param name="currentTiem"></param>
        /// <returns></returns>
        public List<udv_SettleDay> GetSettleDayBll(int AccountID, string currentTiem)
        {
            return dal.GetSettleDayDal(AccountID, currentTiem);
        }

        /// <summary>
        /// 根据平台会员ID获取代理平台预计分成数据
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageLenght"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public List<udv_SettleDay> GetSettleDayForUserID(long userID, int pageIndex, int pageLenght, ref int pageCount)
        {
            return dal.GetSettleDayForUserIDDal(userID, pageIndex, pageLenght, ref pageCount);
        }
        /// <summary>
        /// 插入对象集
        /// </summary>
        /// <param name="Entitys"></param>
        /// <returns></returns>
        public bool Insert(List<SettleDayEntity> Entitys)
        {
            return dal.InsertSettleDay(Entitys);
        }
        /// <summary>
        /// 查询对象集
        /// </summary>
        /// <returns></returns>
        public List<udv_SettleDay> QueryRecharge()
        {
            return dal.QueryRecharge();
        }
        /// <summary>
        /// 查询对象集
        /// </summary>
        /// <returns></returns>
        public List<udv_SettleDay> QueryRefund()
        {
            return dal.QueryRefund();
        }

        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public int InsertEntity(SettleDayEntity Entity)
        {
            return dal.InsertEntity(Entity);
        }

        #region 自定义方法
        /// <summary>
        /// 查询用户收益
        /// </summary>
        /// <param name="UserCode"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public EarningsResult QueryUserEarnings(long UserCode, int PageIndex, int PageSize)
        {
            List<EarningsData> Data = null;
            int Counts = 0;
            int Rec = (int)ResultCode.Success;
            try
            {
                var Entitys = this.GetSettleDayForUserID(UserCode, PageIndex, PageSize, ref Counts);
                if (Entitys != null && Entitys.Count > 0)
                {
                    Data = Entitys.Select(s => new EarningsData()
                    {
                        BuyAmount = s.BuyAmount,
                        Day = s.Day,
                        EarnAmount = Math.Round((double)s.BuyAmount * s.RebateNum / 10000, 2)
                    }).ToList();
                }
                else
                {
                    Counts = 0;
                    Rec = (int)ResultCode.NullData;
                }
            }
            catch (Exception ex)
            {
                Data = null;
                Counts = 0;
                new Log("SettleDayBLL").Write(string.Format("我的收益查询错误：{0}", ex.Message));
            }
            return new EarningsResult()
            {
                Code = Rec,
                Msg = Common.GetDescription((ResultCode)Rec),
                Data = Data,
                Counts = Counts
            };
        }
        #endregion


    }
}
